package org.mclll.xwangplugin.enchantment.item;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.mclll.xwangplugin.enchantment.config.EnchantmentLimitConfig;

import java.util.Map;

/**
 * @author 洛佑希
 */
public class EnchantmentItem {
    public static ItemStack respawnNewItem(ItemStack item) {
        ItemStack itemClone = item.clone();
        // 遍历修改物品的旧附魔信息 创建新附魔表
        if (itemClone.getType() == Material.ENCHANTED_BOOK) {
            // 附魔书需要修改Meta信息
            EnchantmentStorageMeta newMeta = (EnchantmentStorageMeta) itemClone.getItemMeta();
            for (Enchantment bookEnchantment : newMeta.getStoredEnchants().keySet()) {
                if (EnchantmentLimitConfig.limitEnchantments.containsKey(bookEnchantment)) {
                    // 获取限制等级
                    int limitLevel = EnchantmentLimitConfig.limitEnchantments.get(bookEnchantment);
                    if (limitLevel != 0) {
                        // 将限制等级与当前附魔等级的最小值作为附魔等级 覆写到新结果物品
                        newMeta.addStoredEnchant(bookEnchantment, Math.min(limitLevel, newMeta.getStoredEnchantLevel(bookEnchantment)), false);
                    } else {
                        newMeta.removeStoredEnchant(bookEnchantment);
                    }
                }
            }
            if (newMeta.getStoredEnchants().isEmpty()) {
                itemClone = new ItemStack(Material.BOOK);
            } else {
                itemClone.setItemMeta(newMeta);
            }
        } else {
            Map<Enchantment, Integer> oldResultEcm = item.getEnchantments();
            for (Map.Entry<Enchantment, Integer> entry : oldResultEcm.entrySet()) {
                Enchantment enchantment = entry.getKey();
                // 查找该条附魔是否存在限制
                if (EnchantmentLimitConfig.limitEnchantments.containsKey(enchantment)) {
                    // 获取限制等级
                    int limitLevel = EnchantmentLimitConfig.limitEnchantments.get(enchantment);
                    itemClone.removeEnchantment(enchantment);
                    if (limitLevel == 0) {
                        // 检查该附魔是否可以正常挂载到物品上
                    } else if (enchantment.canEnchantItem(itemClone)) {
                        // 将限制等级与当前附魔等级的最小值作为附魔等级 覆写到新结果物品
                        itemClone.addEnchantment(enchantment, Math.min(limitLevel, entry.getValue()));
                    } else {
                        ItemMeta itemMeta = itemClone.getItemMeta();
                        itemMeta.addEnchant(enchantment, Math.min(limitLevel, entry.getValue()), false);
                        itemClone.setItemMeta(itemMeta);
                    }
                }
            }
        }
        return itemClone;
    }

    public static boolean haveLimitEnchant(ItemStack item) {
        if (item.getType() == Material.ENCHANTED_BOOK) {
            EnchantmentStorageMeta newMeta = (EnchantmentStorageMeta) item.getItemMeta();
            for (Enchantment bookEnchantment : newMeta.getStoredEnchants().keySet()) {
                if (EnchantmentLimitConfig.limitEnchantments.containsKey(bookEnchantment)) {
                    return true;
                }
            }
        } else {
            Map<Enchantment, Integer> enchants = item.getEnchantments();
            for (Map.Entry<Enchantment, Integer> entry : enchants.entrySet()) {
                Enchantment enchantment = entry.getKey();
                // 查找该条附魔是否存在限制
                if (EnchantmentLimitConfig.limitEnchantments.containsKey(enchantment)) {
                    return true;
                }
            }
        }
        return false;
    }
}
